#导入蓝图模块
import re
from flask import Blueprint,request,json,jsonify
#注册蓝图模块
bp_city=Blueprint("city",__name__)

#导入数据库
from db import db1

#导入文件操作模块
import os

#导入文件操作的配置文件
from utils.myfile import myfile


#城市表操作
@bp_city.route('/cityM',methods=['POST','GET'])
def cityM():
    if request.method=='GET':
        #构造sql语句
        sql="select * from city"
        res=db1.findall(sql)
        #返回
        return jsonify({'code':200,'list':res})


#文件上传
@bp_city.route('/upload',methods=['POST'])
def upload():
    #获取用户上传的文件
    file=request.files['file']
    print(file)
    #获取用户上传的城市id
    city_id=request.form['city_id']
    #创建文件夹
    myfile.file_mkdir(city_id)
    #图片上传
    file.save(os.path.join('./static/upload/'+str(city_id),file.filename))
    # 返回 图片地址=文件位置+文件名
    return jsonify({'code':200,'url':'/static/upload/'+str(city_id)+'/'+file.filename})




#产品表操作
@bp_city.route('/produatA',methods=['POST','GET'])
def produatA():
    if request.method=='POST':
        data=request.data
        if data:
            data=json.loads(data)
        else:
            data=request.form
        #构造sql语句
        sql="insert into product(name,price,city_id,pic) values('%s',%.2f,%d,'%s')"%(data['name'],float(data['price']),int(data['city_id']),data['pic'])
        db1.insert(sql)
        #返回
        return jsonify({'code':200})

    elif request.method=='GET':
        #获取sort
        sort=request.args.get('sort')
        #获取当前页
        page=request.args.get('page')
        #每页显示多少条
        page_size=2
        #计算开始位置下标
        start = (int(page)-1)*page_size
        # 根据城市id(cid)查询分类下的商品
        city_id=request.args.get('city_id')
        #定义总条数为0
        total=0
        print(city_id)
        if city_id=='0':
            #构造sql语句
            sql="select * from product order by price %s limit %d,%d"%(sort,start,page_size)
            print(sql)

            db1.findall(sql)
            # print(sql)
            #查询总条数
            sql1="select count(id) as c from product"
            res=db1.find(sql1)
            total=res['c']
        else:
            #构造sql语句
            sql="select * from product  where city_id=%d order by price %s limit %d,%d"%(int(city_id),sort,start,page_size)
            print(sql)
            #查询总条数
            sql2="select count(id) as c from product where city_id=%d"%(int(city_id))
            res=db1.find(sql2)
            total = res['c']

        res=db1.findall(sql)
        #返回
        return jsonify({'code':200,'list':res,'total':total})


#获取分类接口
@bp_city.route('/ypM',methods=['POST','GET'])
def ypM():
    if request.method=='GET':
        #构造sql语句
        sql="select * from yptype"
        res=db1.findall(sql)
        #返回
        return jsonify({'code':200,'list':res})


#导入时间模块
import datetime
#获取商品接口
@bp_city.route('/reM',methods=['POST','GET'])
def reM():
    if request.method=='GET':
        #获取前端传递的分类id,typt_id
        type_id=request.args.get('type_id')
        if type_id=='0':
            #构造sql语句如果type_id不存在去数据库获取全部数据
            sql="select * from record"
        else:
            #构造sql语句 type_id存在根据type_id查询数据
            sql="select * from record where type_id=%d"%(int(type_id))
        res=db1.findall(sql)
        
        #定义一个空列表
        list=[]
        #循环
        for i in res:
        #把数据转换成字典格式
            dict={'id':i['id'],'stime':datetime.datetime.strftime(i['stime'],'%Y-%m-%d'),'endtime':datetime.datetime.strftime(i['endtime'],'%Y-%m-%d'),'money':i['money']}
            list.append(dict)
        #返回
        return jsonify({'code':200,'list':list})








